// TODO: 类与对象
// DATE: 2022/3/18
// AUTHOR: Cheng Ze WUST
class testA{
    var name="nobody"
    val number:Int
        get() = (1..1000).shuffled().first()    //给出1-1000的随机值返回给getNumber()函数

    var strn:String?="" //null
    fun getShowStrn():String{   //防范竞态条件，KT规范化
        return strn?.let {
            if (it.isBlank()){
                "--str1为空值--"
            }else{
                "--$it"
            }
        }?:"此值为null"
    }
}

class testB(_name:String,_age:Int,_sex:Char){
    private var name=_name
    var age=_age
    val sex=_sex
    fun showName():String{
        return name
    }
}

class testC(name:String="Zip") {  //主构造
    init {  //初始构造代码块
        println("初始化时会执行该内容")
    }
    //region 懒加载
    //延迟初始化，只能是var。用的时候再加载(需要手动加载)
    lateinit var liStr:String
    fun readliStr(){
        liStr="liStr has ready"
    }

    //惰性加载，以自动方式加载
    val dcStr by lazy { readdcStr() }
    private fun readdcStr():String{
        return "dcStr has ready"
    }
    //endregion

    //次构造必须要调用主构造(主构造统一管理，为了更好的初始化设计)
    constructor(name: String,info:String):this(name){
        println("c$name,$info")
    }
    constructor(name: String,info:String="ookk",age:Int):this(name){
        println("cc$name,$info,$age")
    }
    fun show(){
        println("nothing")
    }
}


fun main(){
    //region testA
    var test1:testA = testA()
    test1.name="Cheng"
    println(test1.name)
    //test1.number=16   //val没有set函数
    println(test1.number)
    println(test1.getShowStrn())
    //endregion

    //region testB
    var testb:testB=testB("Cheng",22,'男')
    println(testb.showName())
    //endregion

    //region testC
    var testc:testC=testC("Lee","2333")
    testC() //优先调用主构造
    testc.show()
    testc.readliStr()
    println(testc.liStr)
    println(testc.dcStr)
    //endregion
}


